public class Leetcode169 {

    public static void main(String[] args) {
        System.out.println(majorityElement(new int[]{2,2,1,1,1,2,2}));
    }

    public static int majorityElement(int[] nums) {
        int number = nums[0];
        int count = 0;

        for (int num : nums) {
            if (num == number) {
                count++;
            } else {
                count--;
                if (count == 0) {
                    number = num;
                    count = 1;
                }
            }
        }

        return number;
    }

}
